/*
 * File Name：CWPanel.java
 * Copyright：Copyright 2008-2013 CiWong.Inc. All Rights Reserved.
 * Description： CWPanel.java
 * Modify By：PLA-ZJLIU
 * Modify Date：2013-1-11
 * Modify Type：Add
 */
package com.ciwong.ciwongwrite.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.ciwong.ciwongwrite.R;


/**
 * 
 * <pre>
 * </pre>
 * 
 * @author PLA-ZJLIU
 * @version ciwong v.1.0 2013-1-11
 * @since ciwong v.1.0
 */
public class CWPanel extends LinearLayout
{
    private static final int HOLE_MARGIN = 48;

    private static final int HOLE_MARGIN_LEFT = 20;

    private static final int PAPER_LINE_HIGHT = 60;

    private static final int PAPER_LINE_WIDTH = 1;
    private int pager;

    private Paint linePaint;

    public CWPanel(Context context)
    {
        super(context);
        init(context);
    }

    public CWPanel(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    public CWPanel(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context mContext)
    {
        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setDither(true);
        linePaint.setColor(0xffDED9CB);
        // linePaint.setStrokeWidth(PAPER_LINE_WIDTH);
        //setPadding(mBitmap.getWidth(), 0, 0, 0);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        invalidate();
    }

    public void setPager(int paper)
    {
        this.pager = paper;
        invalidate();
    }

    /**
     * 获取日志涂鸦内容
     * 
     * @param paintBitmap
     * @return
     */
    public Bitmap generateJournal(Bitmap paintBitmap)
    {
        final int topBottom = 20;
        final int bh = 0;
        final int ph = paintBitmap.getHeight() - topBottom * 2;
        final int drawCount = ph / (bh + HOLE_MARGIN * 2) + 1;
        Bitmap retBitmap = Bitmap.createBitmap(paintBitmap.getWidth(),
                paintBitmap.getHeight(), Config.ARGB_8888);
        Canvas mCanvas = new Canvas(retBitmap);
        mCanvas.drawBitmap(paintBitmap, 0, 0, null);
        return retBitmap;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        final int h = getMeasuredHeight();
        final int topBottom = 20;
        final int bh = 0;
        final int ph = h - topBottom * 2;
        final int lineCount = h - getChildAt(0).getMeasuredHeight()
                / (PAPER_LINE_HIGHT + PAPER_LINE_WIDTH);
        final int drawCount = ph / (bh + HOLE_MARGIN * 2) + 1;
       if(pager==1){// 数学格子
           drawMathLine(lineCount, canvas);
       }
    }


    /**
     * 纸张类型
     * 
     * @return
     */
    public int getPager()
    {
        return pager;
    }

    private void drawMathLine(int lineCount, Canvas mCanvas)
    {
        final int w = getMeasuredWidth();
        int startY = getChildAt(0).getMeasuredHeight();
        int startX = HOLE_MARGIN_LEFT  + 5;
        for (int i = 0; i < lineCount; i++)
        {
            startY += PAPER_LINE_HIGHT;
            mCanvas.drawLine(startX, startY, w - 8, startY + PAPER_LINE_WIDTH, linePaint);
            startY += PAPER_LINE_WIDTH;
        }
    }
}
